﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class XF_SFXXInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        DropDownList ddl信访类别, ddl区属;
        TextBox txtID, txtPNID;
        MPPersistence Persistence1;
        Panel panel1;
        Button btnUpload, btn_Insert, btnReturn;
        TextBox 征收许可证号, 项目名称, 征收时间, 征收地点;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_XINFANG";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
                QSDataDLL();
            }
            Persistence1.BindControl = panel1;
            btnUpload.Click += new EventHandler(btnUpload_Click);
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            btnReturn.Click += new EventHandler(btnReturn_Click);
            IsLookPerson();
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }

        protected void InitControl()
        {
            Persistence1 = (MPPersistence)FindControl("Persistence1");
            panel1 = (Panel)FindControl("panel1");
            ddl信访类别 = (DropDownList)FindControl("ddl信访类别");
            txtID = (TextBox)FindControl("txtID");
            txtPNID = (TextBox)FindControl("txtPNID");
            btnUpload = (Button)FindControl("btnUpload");
            btn_Insert = (Button)FindControl("btn_Insert");
            btnReturn = (Button)FindControl("btnReturn");

            征收许可证号 = (TextBox)FindControl("征收许可证号");
            项目名称 = (TextBox)FindControl("项目名称");
            征收时间 = (TextBox)FindControl("征收时间");
            征收地点 = (TextBox)FindControl("征收地点");

            ddl区属 = (DropDownList)FindControl("ddl区属");
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!string.IsNullOrEmpty(base.Page.Request["type"]))
            {
                btnUpload.Visible = false;
                btn_Insert.Visible = false;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "hidebutton", "<script>hidebutton();</script>");
            }
            string pnid = base.Page.Request["PNID"];
            if (!string.IsNullOrEmpty(pnid))
            {
                //EluZSProvider.Prono prono = _EluZSDataProvider.GetProno(pnid);
                //征收许可证号.Text = prono.征收许可证号;
                //string pid = prono.PID;
                //if (!string.IsNullOrEmpty(pid))
                //{
                    
                //}
                EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(pnid);
                if (project.Addtime.HasValue) { 征收时间.Text = project.Addtime.Value.ToShortDateString(); }
                项目名称.Text = project.项目名称;
                征收地点.Text = project.项目地址;
                征收许可证号.Text = _EluZSDataProvider.GetString("select 征收许可证号 from zs_prono where pid='" + pnid + "'");
            }
            if (string.IsNullOrEmpty(Persistence1.KeyValue))
            {
                btnUpload.Attributes.Add("style", "display:none");
            }
        }
        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            Persistence1.Update();
            string sql = "select pid from zs_prono where id='" + txtPNID.Text.Trim() + "'";
            string pid = _EluZSDataProvider.GetString(sql);
           // HttpContext.Current.Response.Redirect("XF_SFXXInfo.aspx?ID=" + Persistence1.KeyValue + "&PNID=" + txtPNID.Text + "&PID=" + pid + "");
            if (!string.IsNullOrEmpty(base.Page.Request["type"]))
            {
                base.Page.Response.Redirect("XF_SFXX.aspx?type=" + base.Page.Request["type"] + "");
            }
            else
            {
                base.Page.Response.Redirect("XF_SFXX.aspx");
            }
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(base.Page.Request["type"]))
            {
                base.Page.Response.Redirect("XF_SFXX.aspx?type=" + base.Page.Request["type"] + "");
            }
            else
            {
                base.Page.Response.Redirect("XF_SFXX.aspx");
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //设置文件上传的关键字段
            if (!string.IsNullOrEmpty(Persistence1.KeyValue))
            {
                if (true)
                {

                }
                string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}');</script>",
                    Persistence1.KeyValue, base.Page.Request["ID"].Trim(), base.Page.Request["PID"].Trim());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }
        protected void QSDataDLL()
        {
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("信访类别");
            EluZSProvider.Params item = new EluZSProvider.Params();
            _params.Insert(0, item);
            ddl信访类别.DataSource = _params;
            ddl信访类别.DataValueField = "keyname";
            ddl信访类别.DataTextField = "keyname";
            ddl信访类别.DataBind();


            string sql = "Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%'";
            string g = _EluZSDataProvider.GetString(sql);
            if (g == "市征收办(信访科) " || g == "超级管理员")
            {
                List<EluZSProvider.Params> _params1 = _EluZSDataProvider.GetParams("区属");
                EluZSProvider.Params item1 = new EluZSProvider.Params();
                ddl区属.DataSource = _params1;
                ddl区属.DataValueField = "keyname";
                ddl区属.DataTextField = "keyname";
                ddl区属.DataBind();
            }
            else
            {
                DataTable dt = new DataTable();
                if (_EluZSDataProvider.IsQZSBM(userid))
                {
                    if (g.IndexOf("玄武区") > -1)
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", " and keyname='白下区'");

                    }
                    else if (g.IndexOf("下关区") > -1)
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", " and keyname='鼓楼区'");

                    }
                    else
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%' ");
                    }
                    ddl区属.DataSource = dt;
                    ddl区属.DataValueField = "keyname";
                    ddl区属.DataTextField = "keyname";
                    ddl区属.DataBind();
                }

            }
        }
    }
}


